/**
 * Description:
 * Class Name:FileLockTest
 * Date:2022/5/3 19:40
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第15章_输入输出._15_9_NIO._15_9_5_文件锁;

import java.io.FileOutputStream;
import java.nio.channels.FileLock;

public class FileLockTest {
    public static void main(String[] args) {
        try (
                var channel = new FileOutputStream("Chapter_15_9_3_输出测试.txt")
                ){
            FileLock lock = channel.getChannel().tryLock();
            // 程序暂停10s，这10秒内其他程序无法改动该文件
            Thread.sleep(1000);
            // 释放锁
            lock.release();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
